package com.org.cys.controller.${package};

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.org.cys.controller.base.BaseController;
import com.org.cys.pojo.base.Page;
import com.org.cys.service.${package}.${entityName}Service;
import com.org.cys.util.ExportExcel;
import com.org.cys.util.PageData;  
  
  
@Controller  
@RequestMapping("/${entityNameSmall}")  
public class ${entityName}Controller extends BaseController{  
    
	private static Logger logger = Logger.getLogger(${entityName}Controller.class);
	
	@Resource(name="${entityNameSmall}ServiceImpl")
    private ${entityName}Service ${entityNameSmall}Service;  
	
   
   /**
     * 去用户列表页面
     * @author caiyushen
     * */
    @RequestMapping(value="/toList",method = RequestMethod.GET)  
    public String toList() throws Exception{  
        return "${entityNameSmall}/list";  
    }  
	
	
    /**
     * 获取列表信息
     * @param page 分页实体类
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public PageData list(Page page){
    	PageData pd = new PageData();
    	try{
    		pd = this.getPageData();
    		page.setPd(pd);
    		logger.info("method list() param: " + page.toString());
    		List<PageData> dataList = ${entityNameSmall}Service.dataList(page);
    		pd.put("dataList", dataList);
    		pd.put("total",page.getTotalResult());
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    	}
    	return pd;
    }
    
    /**
     * 新增${entityName}
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public PageData add(@RequestBody PageData pd){
    	logger.info("method add() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		${entityNameSmall}Service.save(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
   
    
    /**
     * 修改${entityName}
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public PageData update(@RequestBody PageData pd){
    	logger.info("method update() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		${entityNameSmall}Service.update(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /**
     * 删除${entityName}
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public PageData delete(@RequestBody PageData pd){
    	logger.info("method delete() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		${entityNameSmall}Service.delete(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /**
     * 导出Excel数据
     * */
    @RequestMapping(value="/exportExcel",method = RequestMethod.GET)
    public ModelAndView exportExcel(){
    	ModelAndView mv = new ModelAndView();
    	PageData pd = new PageData();
    	pd = this.getPageData();
    	try{
    		Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
		<#list dataList as var>
			<#if var.newColumn != "id">	
		    titles.add("${var.comment}"); //${var_index+1}	
		    </#if>
		</#list>
			dataMap.put("titles", titles);
			List<PageData> varOList = ${entityNameSmall}Service.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
		<#list dataList as var>		
			<#if var.newColumn != "id">	
				vpd.put("var${var_index+1}", varOList.get(i).getString("${var.newColumn}"));//${var_index+1}
			</#if>
		</#list>	
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ExportExcel erv = new ExportExcel();
			mv = new ModelAndView(erv,dataMap);
    	}catch(Exception e){
			logger.error(e.toString(), e);
		}
    	
    	return mv;
    }
    
    /**
     * 批量删除
     * @author caiyushen
     * */
/*    @ResponseBody
    @RequestMapping(value="/deleteAll",method = RequestMethod.DELETE)
	public PageData deleteAll(@RequestBody PageData pd){
    	logger.info("method deleteAll() param: "+pd.toString());
    	try {
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				${entityNameSmall}Service.deleteAll(ArrayDATA_IDS);
				pd.put("success", true);
			}else{
				pd.put("success", false);
			}
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return pd;
    }
 */   
    
    
    
}  